<?php
uses('Flay');
uses("sanitize");
//$isModuleRender - flag that it's module element render.
if(!isset($isModuleRender))
{
	$isModuleRender = false;
}
$reviewConfig = cr('ReviewConfig');
$presentUrl =  $this->params['controller'].'-'.$this->params['action'];
if($isModuleRender == true)
{
	$presentUrl = "";	
}
$showVotes = true;
$showOtherLinks = true;
$showCustomFields = false;
if(!isset($showAvatar))
{
  $showAvatar = false;
}
if($isModuleRender == false && ($presentUrl == 'reviews-view' || (isset($this->params['pass']['0']) && $this->params['pass']['0'] == 'listing')))
{
	$showAvatar = true;
	$showCustomFields = true;
}
$authedUserId = $session->read('Auth.User.id');
if(!isset($this->Review))
{
	$this->Review = cri('Review');
	$this->Review->setScope('published');
}

//code to set css class name starts here
$themeClass = $view_type;
$containerClass = '';
if($view_type == 'table_view')
{
	$themeClass = 'table_view';
	$containerClass = 'table_view_container';
}
//code to set css class name ends here
?>

<?php
if(empty($reviews))
{
	echo "<h4 class='no_results'>".__("No reviews found!")."</h4>";
}
else
{
	foreach($reviews as $review)
	{
		$reviewClass = "";
		if(isset($review['Review']['is_featured']) && $review['Review']['is_featured'] == 1)
		{
			$reviewClass = "featured";	
		}
		?>
		<li style="list-style:none;">
			<div class="review_box_lists">
				<div class="blog_view">
					<?php if($showAvatar){?>
					<div class="poster_info">
					<?php if($review['Review']['poster_entity'] == 'Guest')
					{?>
						<?php echo $html->image($review['Review']['poster']['avatar'],array("thumb"=>array('height'=>"94",'width'=>"94"),"class"=>"img-circle"))?>	
					<?php }else{?>
						<?php echo $html->link($html->image($review['Review']['poster']['avatar'],array("thumb"=>array('height'=>"94",'width'=>"94"),"class"=>"img-circle")), $review['Review']['poster']['profile_url'], array('escape'=>false,'title'=>$review['Review']['poster']['display_name'],'class' => 'poster')) ?>
					<?php }?>
					</div>
					<?php } ?>
					<div class="desc"> 
						<div class="desc_l">
							<div class="tt_desc">                                            
							  <h5><?php echo $html->link($review['Review']['title'],$review['Review']['viewUrl']);?></h5>
							  <p><?php echo __("Written By") ?>: 
							  	<?php if($review['Review']['poster_entity'] == 'Guest'){?>
									<?php echo $review['Review']['poster']['display_name'];?>
								<?php }else{?>
									<?php echo $html->link($review['Review']['poster']['display_name'], $review['Review']['poster']['profile_url'], array('class' => 'poster')) ?>
								<?php }?> 
								<?php echo $time->timeAgoInwords($review['Review']['created'], array('shortString' => true)) ?> </p> 
							</div>
							<?php
							if(!isset($reviewInformation))
							{
								$html->initHelper("ReviewInformation");
								$reviewInformation = $html->ReviewInformation;
							 }
							if ($showCustomFields)
							{
							  echo $reviewInformation->showCustomFields($review);
							}
							?>
							<div class="overall_rating"> 
							  <?php
								  $rateableFields = $reviewInformation->getRateableFields($review);
								  echo $reviewInformation->showAverageRating($review,__("Overall rating"));
								?>
								<?php
								if ($presentUrl == 'reviews-view' && count($rateableFields) > 1)
								{
									echo $reviewInformation->showDetailedRatings($review);
								}
								?>
							</div><!--overall_rating--> 
						</div>  
					</div> <!--desc-->
					<div class="detail"> 
						<?php
                        $reviewBody = $review['Review']['body'];
						if ($presentUrl != 'reviews-view')
						{
							$reviewBody = strip_tags($review['Review']['body']);
							$reviewBody = str_replace(array("\n",), " ", $reviewBody);
							$reviewBody = Flay::fragment($reviewBody, 120);
						}
						echo $reviewBody;
						echo $html->link(__('Read Full'), $review['Review']['viewUrl'], array('class' => 'read_more')) . " ";
						?>
						
					</div> <!--detail--> 
					<div class="review_bottom_left pull-left">
						<?php if ($showVotes)
						{?>
							<div class="user_vote"><br />
								<?php echo __("Was this review helpful?") ?>
								<?php echo $vote->yahooStyle($review['Review']['id'], 'Review'); ?>
								<br />
							</div>
						<?php
						}?>
						<span style="">
						<?php if(cr('ReviewConfig.upload_assets') && isset($review['Review']['listing_asset_count']) && $review['Review']['listing_asset_count'] > 0){?>
							<?php
									$photoText = "%d photo(s)";
									if($review['Review']['listing_asset_count'] == 1)
									{
										$photoText = "%d photo";
									}
									echo $html->link('<span class="i-camera"></span>'.sprintf(__($photoText),$review['Review']['listing_asset_count']),array("controller"=>"listing_assets",'action'=>"index",'listing','listingId'=>$review['Listing']['id'],'userId'=>$review['Review']['user_id']),array('escape'=>false,'class'=>"photo_count"))?>
						<?php }?>
						</span>
					</div><!--review_bottom_left-->
					<div class="review_bottom_right pull-right"> 
						<span class="report"><?php echo $userContent->report('Review', $review['Review']['id'], 'Report'); ?></span> 
					</div> <!--review_bottom_right-->
				</div> 
			</div><!--review_box_lists-->
		 </li>
		<!--@todo (1.1) Remove class table_view_container when blog view enabled for review index -->
	<?php 
	}//foreach ends here
 }//main if block ends here.
?>
<div class="clear"></div>